{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": "<pyecharts.render.display.Javascript at 0x1744ae87848>",
      "application/javascript": "new Promise(function(resolve, reject) {\n    var script = document.createElement(\"script\");\n    script.onload = resolve;\n    script.onerror = reject;\n    script.src = \"https://assets.pyecharts.org/assets/echarts.min.js\";\n    document.head.appendChild(script);\n}).then(() => {\n\n});"
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "from chanlun import cl\n",
    "from chanlun import kcharts\n",
    "\n",
    "### 避免在 Notebook 中不显示，手动加载下依赖的js\n",
    "###　https://pyecharts.org/#/zh-cn/notebook\n",
    "from pyecharts.charts import Kline, Line, Bar, Grid, Scatter\n",
    "from pyecharts.globals import CurrentConfig, NotebookType\n",
    "\n",
    "CurrentConfig.NOTEBOOK_TYPE = NotebookType.JUPYTER_LAB  # 根据使用的环境设置 notebook 类型， NotebookType.JUPYTER_LAB 或 NotebookType.JUPYTER_NOTEBOOK\n",
    "Kline().load_javascript()\n",
    "Line().load_javascript()\n",
    "Bar().load_javascript()\n",
    "Grid().load_javascript()\n",
    "Scatter().load_javascript()"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 缠论配置项\n",
    "参考项目的 src/chanlun/cl_interface.py 的 Config 对象\n",
    "\n",
    "---\n",
    "\n",
    "### 缠论的默认配置项\n",
    "\n",
    "\n",
    "    # 分型默认配置\n",
    "    'fx_qj': Config.FX_QJ_K.value,\n",
    "    'fx_bh': Config.FX_BH_DINGDI.value,\n",
    "    # 笔默认配置\n",
    "    'bi_type': Config.BI_TYPE_NEW.value,\n",
    "    'bi_bzh': Config.BI_BZH_YES.value,\n",
    "    'bi_qj': Config.BI_QJ_CK.value,\n",
    "    'bi_fx_cgd': Config.BI_FX_CHD_NO.value,\n",
    "    # 线段默认配置\n",
    "    'xd_bzh': Config.XD_BZH_YES.value,\n",
    "    'xd_qj': Config.XD_QJ_DD.value,\n",
    "    # 走势类型默认配置\n",
    "    'zslx_bzh': Config.ZSLX_BZH_YES.value,\n",
    "    'zslx_qj': Config.ZSLX_QJ_DD.value,\n",
    "    # 中枢默认配置\n",
    "    'zs_bi_type': Config.ZS_TYPE_DN.value,  # 笔中枢类型\n",
    "    'zs_xd_type': Config.ZS_TYPE_DN.value,  # 走势中枢类型\n",
    "    'zs_qj': Config.ZS_QJ_DD.value,\n",
    "    'zs_wzgx': Config.ZS_WZGX_ZGD.value,\n",
    "    # 指标配置项\n",
    "    'idx_macd_fast': 12,\n",
    "    'idx_macd_slow': 26,\n",
    "    'idx_macd_signal': 9,\n",
    "    'idx_boll_period': 20,\n",
    "    'idx_ma_period': 5,"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "data": {
      "text/plain": "           code                 date   open  close   high    low     volume\n2395  SH.688122  2022-04-08 11:30:00  82.00  81.50  82.59  81.27   178700.0\n2396  SH.688122  2022-04-08 13:30:00  81.51  81.51  82.08  81.16   180800.0\n2397  SH.688122  2022-04-08 14:00:00  81.52  81.40  81.78  81.12   142800.0\n2398  SH.688122  2022-04-08 14:30:00  81.31  81.72  81.80  80.18   305800.0\n2399  SH.688122  2022-04-08 15:00:00  81.80  82.85  83.03  81.74  1229100.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>code</th>\n      <th>date</th>\n      <th>open</th>\n      <th>close</th>\n      <th>high</th>\n      <th>low</th>\n      <th>volume</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>2395</th>\n      <td>SH.688122</td>\n      <td>2022-04-08 11:30:00</td>\n      <td>82.00</td>\n      <td>81.50</td>\n      <td>82.59</td>\n      <td>81.27</td>\n      <td>178700.0</td>\n    </tr>\n    <tr>\n      <th>2396</th>\n      <td>SH.688122</td>\n      <td>2022-04-08 13:30:00</td>\n      <td>81.51</td>\n      <td>81.51</td>\n      <td>82.08</td>\n      <td>81.16</td>\n      <td>180800.0</td>\n    </tr>\n    <tr>\n      <th>2397</th>\n      <td>SH.688122</td>\n      <td>2022-04-08 14:00:00</td>\n      <td>81.52</td>\n      <td>81.40</td>\n      <td>81.78</td>\n      <td>81.12</td>\n      <td>142800.0</td>\n    </tr>\n    <tr>\n      <th>2398</th>\n      <td>SH.688122</td>\n      <td>2022-04-08 14:30:00</td>\n      <td>81.31</td>\n      <td>81.72</td>\n      <td>81.80</td>\n      <td>80.18</td>\n      <td>305800.0</td>\n    </tr>\n    <tr>\n      <th>2399</th>\n      <td>SH.688122</td>\n      <td>2022-04-08 15:00:00</td>\n      <td>81.80</td>\n      <td>82.85</td>\n      <td>83.03</td>\n      <td>81.74</td>\n      <td>1229100.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取 行情K线数据\n",
    "code = 'SH.688122'\n",
    "frequency = '30m'\n",
    "# 缠论配置\n",
    "cl_config = {'bi_type': 'new', 'xd_bzh': 'xd_bzh_yes', 'zs_bi_type': 'zs_type_dn', 'idx_macd_fast': 12}\n",
    "# 缠论画图配置\n",
    "chart_config = {'show_bi_zs': True, 'show_ma': False}\n",
    "klines = pd.read_csv('./data/688122.csv')\n",
    "klines.tail()\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [],
   "source": [
    "# 依据 K 线数据，计算缠论数据\n",
    "cl_data = cl.CL(code, frequency, cl_config).process_klines(klines)\n",
    "chart = kcharts.render_charts('%s - %s' % (code, frequency), cl_data, config=chart_config)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# 展示图标\n",
    "chart"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": true
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Last Bi :  index: 552 type: down FX: (2022-04-08 11:00:00 - 2022-04-08 14:30:00) high: 82.87 low: 80.18 done: True\n",
      "Last Zs :  index: 110 zs_type: bi level: 0 FX: (2022-03-29 10:30:00-2022-03-31 10:30:00) type: up zg: 87.37 zd: 85.8 gg: 88.5 dd: 84.66 done: True\n"
     ]
    }
   ],
   "source": [
    "# 获取最后一笔\n",
    "bi = cl_data.bis[-1]\n",
    "print('Last Bi : ', bi)\n",
    "# 获取最后一个中枢\n",
    "zs = cl_data.bi_zss[-1]\n",
    "print('Last Zs : ', zs)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "BI MMDS :  []\n",
      "BI BC :  []\n",
      "BI 笔背驰： False\n",
      "BI 盘整背驰： False\n",
      "BI 趋势背驰： False\n"
     ]
    }
   ],
   "source": [
    "# 判断笔是否背驰，是否有买卖点 （只在 Pro 版本支持）\n",
    "bi = cl_data.bis[-11]\n",
    "print('BI MMDS : ', bi.line_mmds())\n",
    "print('BI BC : ', [_bc.type for _bc in bi.bcs if _bc.bc])\n",
    "print('BI 笔背驰：', bi.bc_exists(['bi']))\n",
    "print('BI 盘整背驰：', bi.bc_exists(['pz']))\n",
    "print('BI 趋势背驰：', bi.bc_exists(['qs']))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [],
   "source": [
    "# 买卖点信息 (只在 Pro 版本支持)\n",
    "# mmd = bi.mmds[0]\n",
    "# print(mmd)\n",
    "# print('买卖点对应的中枢：', mmd.zs)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Html 页面展示\n",
    "\n",
    "### 服务端代码\n",
    "    return HttpResponse(kcharts.render_charts('标题', cl_datas))\n",
    "\n",
    "### 页面 Ajax 接收并展示\n",
    "    var chart = echarts.init(document.getElementById('chart_id'), 'drak', {renderer: 'canvas'});\n",
    "    $.ajax({\n",
    "        type: \"GET\",\n",
    "        url: \"/后端请求地址\",\n",
    "        success: function (result) {\n",
    "            var res = (new Function(\"return \" + result))();\n",
    "            chart.setOption(res);\n",
    "        }\n",
    "    });"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}